﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


namespace server
{
    class VANETMessage
    {
        public String senderName { get; set; }	//消息发送者的名字
        public String msg { get; set; }			//信息内容
        public String PseID_1 { get; set; }
        public String PseID_2 { get; set; }	//假名
        public String msg_sign { get; set; }	//消息签名

        /// <summary>
        /// 
        /// </summary>
        public VANETMessage()
        { }

        /// <summary>
        /// VANETMessage类构造函数
        /// </summary>
        /// <param name="jsonstr">JSON字符串</param>
        public VANETMessage(String jsonstr)
        {
            VANETMessage de = (VANETMessage)JsonConvert.DeserializeObject(jsonstr, typeof(VANETMessage));

            senderName = Encoding.GetEncoding("ISO-8859-1").GetString(Convert.FromBase64String(de.senderName));

            msg = Encoding.GetEncoding("UTF-8").GetString(Convert.FromBase64String(de.msg));
            PseID_1 = Encoding.GetEncoding("ISO-8859-1").GetString(Convert.FromBase64String(de.PseID_1));
            PseID_2 = Encoding.GetEncoding("ISO-8859-1").GetString(Convert.FromBase64String(de.PseID_2));
            msg_sign = Encoding.GetEncoding("ISO-8859-1").GetString(Convert.FromBase64String(de.msg_sign));

            // JArray ja = (JArray)JsonConvert.DeserializeObject(jsonstr);

            //JObject o = (JObject)JsonConvert.DeserializeObject(jsonstr);
            //senderName = Encoding.GetEncoding("ISO-8859-1").GetString(Convert.FromBase64String(o["senderName"].ToString()));
            //msg = Encoding.GetEncoding("ISO-8859-1").GetString(Convert.FromBase64String(o["msg"].ToString()));
            //PseID_1 = Encoding.GetEncoding("ISO-8859-1").GetString(Convert.FromBase64String(o["PseID_1"].ToString()));
            //PseID_2 = Encoding.GetEncoding("ISO-8859-1").GetString(Convert.FromBase64String(o["PseID_2"].ToString()));
            //msg_sign = Encoding.GetEncoding("ISO-8859-1").GetString(Convert.FromBase64String(o["msg_sign"].ToString()));
        }

        /// <summary>
        /// 将VANETMessage类对象序列化为JSON格式
        /// </summary>
        /// <returns>JSON字符串</returns>
        public string tostring()
        {
            string tes = JsonConvert.SerializeObject(this);

            return tes;
        }
    }
}
